package day16.edu;

import org.junit.Test;

// Created: 16/09/2025, ChangJiang Ru
public class StringDemo {
    @Test
    public void test1()
    {
        String s1 = "hello";
        String s2 = "hello";

        String s3 = new String("hello");
        String s4 = new String("hello");

        System.out.println(s1 == s2); // true
        System.out.println(s1 == s3); // false
        System.out.println(s3 == s4); // false

        System.out.println(s1.equals(s2)); // true
        System.out.println(s1.equals(s3)); // true
        System.out.println(s3.equals(s4)); // true
    }

    @Test
    public void test2()
    {
        String s1 = "hello";
        String s2 = "world";

        String s3 = "helloworld";
        String s4 = "hello" + "world";
        String s5 = s1 + "world";
        String s6 = s1 + s2;

        System.out.println(s3 == s4); // true
        System.out.println(s3 == s5); // false
        System.out.println(s3 == s6);
        System.out.println(s5 == s6);
        System.out.println(s4 == s6);
    }

    @Test
    public void test3()
    {
        String s1 = new String();
        String s2 = new String("Hello");
        String s3 = new String(new char[]{'h', 'l', 'l'});
        System.out.println(s3);
    }

    @Test
    public void test4()
    {
        int num = 1;
        // Case 1:
        String s1 = num + "";
        System.out.println(s1);

        // Case 2:
        String s2 = String.valueOf(num);
        System.out.println(s2);

        String s3 = "123";
        int i = Integer.parseInt(s3);
        System.out.println(i);
    }
    
    @Test
    public void test5()
    {
        String str = "hello";
        char[] arr = str.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }

    @Test
    public void test6()
    {
        String s1 = "String";
        String s2 = " ";
        boolean b = s1.isEmpty();
        boolean b1 = s2.isEmpty();

        System.out.println(b);
        System.out.println(b1);

        System.out.println(s1.length());
        System.out.println("=========");

        String s3 = "string";
        boolean b2 = s1.equals(s3);
        System.out.println(b2);

        System.out.println("========");
        boolean b3 = s1.equalsIgnoreCase(s3);
        System.out.println(b3);

        System.out.println("=======");
        int i = s1.compareTo(s3);
        System.out.println(i);

        System.out.println("=======");
        int i1 = s1.compareToIgnoreCase(s3);
        System.out.println(i1);

        System.out.println("======");
        String s4 = s1.toLowerCase();
        System.out.println(s4);

        System.out.println("=====");
        String s5 = s3.toUpperCase();
        System.out.println(s5);

        System.out.println("=====");
        String s112 = " hello world ";
        String s111 = s112.trim();
        System.out.println(s111);

        System.out.println("======");
        String num = "123";
        String num1 = new String("123");
        System.out.println(num == num1);
        String num2 = num1.intern();
        System.out.println(num == num2);
        System.out.println(num1 == num2);
        System.out.println(num == num1);

        System.out.println("=====");
        String s6 = "abcd";
        String s7 = "aBed";
        System.out.println(s6.compareTo(s7));
    }

    @Test
    public void test7()
    {
        String s1 = "abcd";
        String s2 = "bc";

        System.out.println(s1.contains("ab"));

        int a = s1.indexOf("bc");
        System.out.println(a);
        int b = s1.indexOf("z");
        System.out.println(b);

        String s3 = "hello";
        int a1 = s3.indexOf("l", 3);
        System.out.println(a1);
    }
    
    @Test
    public void test8()
    {
        String s1 = "We are the family";
        System.out.println(s1.substring(3));

        System.out.println(s1.substring(3, 6));
        System.out.println("==========");

        System.out.println(s1.startsWith("are"));
        System.out.println("=========");
        System.out.println(s1.startsWith("We"));

        System.out.println("==========");
        System.out.println(s1.startsWith("are", 4));

        System.out.println("=======");
        System.out.println(s1.endsWith("family"));
    }
    
    @Test
    public void test9()
    {
        String s1 = "hello";
        String s2 = s1.replace('l', 'w');

        System.out.println(s1);
        System.out.println(s2);

        String s3 = s2.replace("ww", "nnnnnn");
        System.out.println(s3);
    }

    @Test
    public void test10()
    {
        String s1 = "hello";
        s1 = "world";
        System.out.println(s1);
    }
    
    @Test
    public void test11()
    {
        char[] ch = {'t', 'e', 's', 't', 'h', 'a'};
        ch[0] = 'h';
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]);
        }
    }
}
